# 核心思路: 将一维的序列结构转换为二维的键值对结构
class Solution:
    def twoSum(self, nums, target):
        hashmap = {}  # 哈希表
        for ind, num in enumerate(nums):  # 转换为字典结构
            hashmap[num] = ind  # 将列表的数转换为字典
        for i, num in enumerate(nums):  # 遍历每个索引和对应的值
            ###################字典的查找效率远高于列表的查找效率############################
            j = hashmap.get(target - num)  # 第二个数的索引=从字典中获取第二个数的索引
            if j is not None and i != j:  # 如果j存在且不等于i
                return [i, j]  # 返回目标


nums = [2, 7, 11, 15]
target = 9

solution = Solution()
print(solution.twoSum(nums, target))
